const BoomFX = require('BoomFX');

cc.Class({
    extends: cc.Component,

    properties: {
        // 导弹是否发射
        moveable : false,
        // 预置爆炸动画
        prebBoom: cc.Prefab,
        // 导弹位移量
        speed: 0
    },

    // use this for initialization
    onLoad: function () {
		// 开启碰撞检测
        cc.director.getCollisionManager().enabled = true;
        cc.director.getCollisionManager().enabledDebugDraw = true;
        // cc.director.getCollisionManager().enabledDrawBoundingBox = true;
        
        this.touchingNumber = 0;

        this.registerInput();
    },
	
    // 碰撞开始
    onCollisionEnter (other, self) {
        this.node.color = cc.Color.RED;
        this.touchingNumber ++;
        this.moveable = false;
        // this.node.removeFromParent();
        
        let fx = this.spawnBoomFX();
        fx.node.setPosition(this.node.position);
        this.parent.node.addChild(fx.node);
        fx.play();
    },

    // onCollisionStay (other) {
    //     cc.log('on collision stay');
    // },

    onCollisionExit () {
        // this.touchingNumber --;
        // cc.log(this.touchingNumber);
        // if (this.touchingNumber === 0) {
        this.node.color = cc.Color.WHITE;
        // }
    },
    
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        // 导弹未到达目标就继续移动
        if (this.moveable === true) 
        {
            this.moveMissile();
        }
    },
    
    registerInput() {
        let touchLoc,missilePos,dest,dir,rad,deg;
        let self = this;
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            onTouchBegan: function(touch, event) {
                self.missileDir(touch.getLocation());
                return true; // don't capture event
            },
            
            onTouchMoved: function(touch, event) {
                self.missileDir(touch.getLocation());
            },
            
            onTouchEnded: function(touch, event) {
                // 导弹目的地
                self.dest = self.node.parent.convertToNodeSpace(touch.getLocation());

                // 记录导弹起始位置与目标位置距离
                self.startLen = cc.pDistance(self.dest, self.node.position);

                // 记录导弹起始位置
                self.startPos = self.node.position;

                // 导弹是否移动
                self.moveable = true;
            }
        }, self.node);
    },
    
    // 设置导弹发射角度
    missileDir(dest) {
        let self = this.node;
        
        // 计算导弹目标位置与当前导弹位置的向量差
        let dir = cc.pSub(self.parent.convertToNodeSpaceAR(dest), self.position);
        
        // 计算导弹与目标向量弧度
        this.theta = cc.pToAngle(dir);
        
        // 弧度转换为角度
        this.node.rotation = 90 - cc.radiansToDegrees(this.theta);
    },
    
    // 导弹沿直线移动
    moveMissile(){
        // 向量减法，方向由导弹指向目标
        let velocity = cc.pSub(this.dest, this.node.position);

        // 导弹与目标的距离
        let vLength = cc.pLength(velocity);
        
        // 导弹与目标的向量求模
        velocity.x /= vLength;
        velocity.y /= vLength;
        
        // 导弹进行位移
        this.node.x += (velocity.x * this.speed);
        this.node.y += (velocity.y * this.speed);
        
        let lenMoved = cc.pDistance(this.node.position, this.startPos);
        
        if(lenMoved > this.startLen)
        {
            this.moveable = false;
        }
    },
    
    // 生成爆炸效果
    spawnBoomFX: function () {
        var fx;
        if (cc.pool.hasObject(BoomFX)) 
        {
            fx = cc.pool.getFromPool(BoomFX);
            return fx;
        } 
        else 
        {
            fx = cc.instantiate(this.prebBoom);
            return fx.getComponent('BoomFX');
        }
    },
    
});
